package com.hydl.power.controller;

import com.github.pagehelper.PageInfo;
import com.hydl.power.aop.annotation.LogAnnotation;
import com.hydl.power.mapper.SysUserMapper;
import com.hydl.power.pojo.Maintainer;
import com.hydl.power.pojo.SysUser;
import com.hydl.power.service.MaintainerService;
import com.hydl.power.service.PowerRoomService;
import com.hydl.power.service.SysUserService;
import com.hydl.power.utils.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

@Api(value = "MaintainerController",tags = "运维人员控制类")
@RequestMapping("/maintainer")
@RestController
@CrossOrigin
public class MaintainerController {
    @Autowired
    private MaintainerService maintainerService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private PowerRoomService powerRoomService;


    //@LogAnnotation(module = "运维人员控制类",description = "查询运维人员列表")
    @ApiOperation("查询运维人员列表")
    @PostMapping("/getAllMaintainer")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数", dataType = "Integer"),
    })
    public Resp<PageInfo<Maintainer>> selectDevice(Integer pageNum, Integer pageSize) {
        PageInfo<Maintainer> list = maintainerService.selectDevice(pageNum, pageSize);
        return Resp.success(list);
    }


    @LogAnnotation(module = "运维人员控制类",description = "新增运维人员")
    @PostMapping("/addMaintainer")
    @ApiOperation(value = "新增运维人员", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "card", value = "卡号",dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "phone", value = "电话",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "userId", value = "userID",dataType = "Integer")
    })
    public Serializable addMaintainer(Integer card,String phone,Integer userId) {
        List<Maintainer> maintainerList = maintainerService.getAll();
        SysUser sysUser = sysUserMapper.getAllById(userId);
        for (int i = 0;i < maintainerList.size();i++){
            if (sysUser.getUsername().equals(maintainerList.get(i).getName())){
                return Resp.fail("运维人员已存在");
            }
        }
        int i = maintainerService.addMaintainer(card, phone, userId);
        if (i == 1){
            return Resp.success("添加成功");
        }
        return Resp.fail("添加失败");
    }


    // @LogAnnotation(module = "运维人员控制类",description = "获取user列表")
    @ApiOperation("获取user列表")
    @GetMapping("/getAllUser")
    public Resp<List<SysUser>> getAllUser() {
        return Resp.success(sysUserService.getAllUser());
    }



    @LogAnnotation(module = "运维人员控制类",description = "删除运维人员")
    @PostMapping("/deleteMaintainer")
    @ApiOperation(value = "删除运维人员", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "运维id",dataType = "Integer")
    })
    public Resp deleteMaintainer(Integer id) {
        int i = maintainerService.deleteMaintainerStatus(id);
        if (i == 1){
            return Resp.success("删除成功");
        }
        return Resp.fail("删除失败");
    }


    //@LogAnnotation(module = "运维人员控制类",description = "根据ID获取运维人员")
    @ApiOperation(value="根据ID获取运维人员")
    @PostMapping("/selectMaintainerById")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "运维人员id",dataType = "Integer"),
    })
    public Resp selectMaintainerById(Integer id){
        return Resp.success(maintainerService.selectById(id));
    }


    @LogAnnotation(module = "运维人员控制类",description = "修改运维人员")
    @PostMapping("/updateMaintainer")
    @ApiOperation(value = "修改运维人员", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "运维id",dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "card", value = "卡号",dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "phone", value = "电话",dataType = "String"),
    })
    public Resp updateMaintainer(Integer id,Integer card, String phone) {
        int i = maintainerService.updateMaintainer(id, card, phone);
        if (i == 1){
            return Resp.success("修改成功");
        }
        return Resp.fail("修改失败");
    }


    @LogAnnotation(module = "运维人员控制类",description = "运维人员批量增加/修改配电房")
    @PostMapping("/addMaintainerAndPowerroom")
    @ApiOperation(value = "运维人员批量增加/修改配电房", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "maintainerId", value = "运维长id",dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "powerroomIds", value = "配电房powerroomIds[]",dataType = "Integer"),
    })
    public Resp addMaintainerAndPowerroom(String maintainerId, String powerroomIds){
        int i = maintainerService.addMaintainerAndPowerroom(maintainerId, powerroomIds);
        if (i != 0){
            return Resp.success("添加成功");
        }
        return Resp.fail("添加失败");
    }


    // @LogAnnotation(module = "运维人员控制类",description = "获取配电房")
    @GetMapping("/getAllPowerRoomForList")
    @ApiOperation(value = "获取配电房", produces = "application/json")
    public Resp getAllPowerRoomForList(){
        return Resp.success(powerRoomService.all());
    }
}

